package com.ruoyi.web.controller.supermarket;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.supermarket.domain.SupermarketCommodity;
import com.ruoyi.supermarket.domain.SupermarketSale;
import com.ruoyi.supermarket.service.ISupermarketCommodityService;
import com.ruoyi.supermarket.service.ISupermarketSaleService;
import io.netty.util.internal.ObjectUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 销售信息Controller
 * 
 * @author faye
 * @date 2024-05-27
 */
@RestController
@RequestMapping("/supermarket/sale")
public class SupermarketSaleController extends BaseController
{
    @Autowired
    private ISupermarketSaleService supermarketSaleService;

    @Autowired
    private ISupermarketCommodityService commodityService;

    /**
     * 查询销售信息列表
     */
    @PreAuthorize("@ss.hasPermi('supermarket:sale:list')")
    @GetMapping("/list")
    public TableDataInfo list(SupermarketSale supermarketSale)
    {
        startPage();
        List<SupermarketSale> list = supermarketSaleService.selectSupermarketSaleList(supermarketSale);
        return getDataTable(list);
    }

    @GetMapping("/echarts/profitBie")
    public AjaxResult profitBie()
    {
        SupermarketSale sale=new SupermarketSale();

        List<SupermarketSale> list = supermarketSaleService.selectSupermarketSaleList(sale);

        Map<String,Double> saleDataCol = new HashMap<>();
        Map<Long,Double> map1=list.stream().filter(item-> ObjectUtils.isNotEmpty(item.getSupermarketCommodity().getCommodityName())
        ).collect(Collectors.groupingBy(SupermarketSale::getSaleCommodityId,Collectors.summingDouble(SupermarketSale::getSaleProfit)));
        map1.forEach((key,value)->{
            saleDataCol.put(commodityService.selectSupermarketCommodityByCommodityId(key).getCommodityName()
                    ,value);
        });
        List<Map<String,Object>> mapList=new ArrayList<>();
      // System.out.println(saleDataCol);
        if(!saleDataCol.isEmpty()){
            for(String key:saleDataCol.keySet()){
                Map<String,Object> map=new HashMap<>();
                map.put("name",key);
                map.put("value",saleDataCol.get(key));
                mapList.add(map);
            }
        }
        return success(mapList);
    }




    @GetMapping("/echarts/bie")
    public AjaxResult bie()
    {
        SupermarketSale sale=new SupermarketSale();

        List<SupermarketSale> list = supermarketSaleService.selectSupermarketSaleList(sale);
        System.out.println(list);
        Map<String,Long> saleDataCol = new HashMap<>();
        Map<Long,Long> map1=list.stream().filter(item-> ObjectUtils.isNotEmpty(item.getSupermarketCommodity().getCommodityName())
        ).collect(Collectors.groupingBy(SupermarketSale::getSaleCommodityId,Collectors.summingLong(SupermarketSale::getSaleQuantity)));
                       map1.forEach((key,value)->{
                               saleDataCol.put(commodityService.selectSupermarketCommodityByCommodityId(key).getCommodityName()
                                       ,value);
                       });
        List<Map<String,Object>> mapList=new ArrayList<>();
        System.out.println(saleDataCol);
        if(!saleDataCol.isEmpty()){
            for(String key:saleDataCol.keySet()){
                Map<String,Object> map=new HashMap<>();
                map.put("name",key);
                map.put("value",saleDataCol.get(key));
                mapList.add(map);
            }
        }

        return success(mapList);
    }


    /**
     * 导出销售信息列表
     */
    @PreAuthorize("@ss.hasPermi('supermarket:sale:export')")
    @Log(title = "销售信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SupermarketSale supermarketSale)
    {
        List<SupermarketSale> list = supermarketSaleService.selectSupermarketSaleList(supermarketSale);
        ExcelUtil<SupermarketSale> util = new ExcelUtil<SupermarketSale>(SupermarketSale.class);
        util.exportExcel(response, list, "销售信息数据");
    }

    /**
     * 获取销售信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('supermarket:sale:query')")
    @GetMapping(value = "/{saleId}")
    public AjaxResult getInfo(@PathVariable("saleId") Long saleId)
    {
        return success(supermarketSaleService.selectSupermarketSaleBySaleId(saleId));
    }

    /**
     * 新增销售信息
     */
    @PreAuthorize("@ss.hasPermi('supermarket:sale:add')")
    @Log(title = "销售信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SupermarketSale supermarketSale)
    {
        return toAjax(supermarketSaleService.insertSupermarketSale(supermarketSale));
    }

    /**
     * 修改销售信息
     */
    @PreAuthorize("@ss.hasPermi('supermarket:sale:edit')")
    @Log(title = "销售信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SupermarketSale supermarketSale)
    {
        return toAjax(supermarketSaleService.updateSupermarketSale(supermarketSale));
    }

    /**
     * 删除销售信息
     */
    @PreAuthorize("@ss.hasPermi('supermarket:sale:remove')")
    @Log(title = "销售信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{saleIds}")
    public AjaxResult remove(@PathVariable Long[] saleIds)
    {
        return toAjax(supermarketSaleService.deleteSupermarketSaleBySaleIds(saleIds));
    }
}
